<template>
	<view class="uploaddocuments">
		<view class="uploaddocuments-conten">
			<view class="uploaddocuments-titlt">
				证件信息
			</view>
			<view class="uploaddocuments-up">
				<view class="uploaddocuments-up-list">
					<image-model :data="form" :list="driverLicenseListData" @update="update" />
				</view>
			</view>
		<!-- 	<view class="inp-conten">
				<l-form-cell label="身份证有效截止日期" label-width="290rpx" has-slot :arrow="!disabled" :unlined="!disabled" :must="true" @click="changeDate('idCardValidDateEnd')">
				  <view class="form-item flex-row form-item align-center justify-end">
				    <text class="text-30" :class="['color-t' + (idCardValidDateEnd ? 1 : 3)]">{{ idCardValidDateEnd || '请选择日期' }}</text>
				  </view>
				</l-form-cell>
			</view>
			<tui-datetime ref="dateTime" :cancel-color="colorError" :type="2" :show-cancel="!!cancelText" :cancel-text="cancelText" :end-year="2100" :set-date-time="setDateTime" @confirm="setDate" @cancel="setDate" /> -->
		</view>
		<view class="btn">
			<view class="sucess-btn" @click="seucessFn">
				确定更新
			</view>
		</view>
	</view>
</template>

<script>
	import { submitCertificatesStatus } from '@/common/api/driver.js'
	export default {
		data(){
			return {
				idCardValidDateEnd:'',
				driverLicenseListData:[],
				driverLicenseList: [
				  { attr: 'idCard1', label: '身份证正面', default: 'https://tbb-file.oss-cn-shenzhen.aliyuncs.com/tbb-portal/static/idcard.png' },
				  { attr: 'idCard2', label: '身份证反面', default: 'https://tbb-file.oss-cn-shenzhen.aliyuncs.com/tbb-portal/static/idcardback.png' },
				  { attr: 'licence1', label: '驾驶证正本', default: 'https://tbb-file.oss-cn-shenzhen.aliyuncs.com/tbb-portal/static/singshizheng.png'},
				  { attr: 'certification', label: '从业资格证', default: 'https://tbb-file.oss-cn-shenzhen.aliyuncs.com/tbb-portal/static/zigezheng.png'},
				  { attr: 'thirdYearNotMajorAccidentCert', label: '三年无重大事故证明', default: 'https://tbb-file.oss-cn-shenzhen.aliyuncs.com/tbb-portal/static/zigezheng.png'}
				],
				newDriverLicenseList:[],
				form: {
					idCard1:'',
					idCard2:'',
					licence1:'',
					certification:'',
					vlicencePicture:'',
					bizPicture:'',
					thirdYearNotMajorAccidentCert:''
				},
				certificationType:'',
				type:0,
				id:'',
			}
		},
		onLoad(options) {
			this.id = options.id
			this.type = options.type
			let data = []
			switch (Number(this.type)){
				case 0:
				data = [
					{ attr: 'idCard1', label: '身份证正面', default: 'https://tbb-file.oss-cn-shenzhen.aliyuncs.com/tbb-portal/static/idcard.png' },
					{ attr: 'idCard2', label: '身份证反面', default: 'https://tbb-file.oss-cn-shenzhen.aliyuncs.com/tbb-portal/static/idcardback.png' },
				]
					this.driverLicenseListData.push(...data)
				break;
				case 1:
				data = [
					{ attr: 'licence1', label: '驾驶证正本', default: 'https://tbb-file.oss-cn-shenzhen.aliyuncs.com/tbb-portal/static/singshizheng.png'},
				]
					this.driverLicenseListData.push(...data)
					break;
				case 2:
				data = [
					{ attr: 'certification', label: '从业资格证', default: 'https://tbb-file.oss-cn-shenzhen.aliyuncs.com/tbb-portal/static/zigezheng.png'},
				]
					this.driverLicenseListData.push(...data)
					break;
				case 3:
				data = [
					{ attr: 'vlicencePicture', label: '行驶证', default: 'https://tbb-file.oss-cn-shenzhen.aliyuncs.com/tbb-portal/static/singshizheng.png'},
				]
					this.driverLicenseListData.push(...data)
					break;
				case 4:
				data = [
					{ attr: 'bizPicture', label: '道路运输证更新', default: 'https://tbb-file.oss-cn-shenzhen.aliyuncs.com/tbb-portal/static/zigezheng.png'},
				]
					this.driverLicenseListData.push(...data)
					break;
				case 5:
				data = [
					{ attr: 'thirdYearNotMajorAccidentCert', label: '上传三年无重大事故证明', default: 'https://tbb-file.oss-cn-shenzhen.aliyuncs.com/tbb-portal/static/zigezheng.png'},
				]
					this.driverLicenseListData.push(...data)
					break;
				default:
					break;
			}
		},
		methods: {
			changeDate(attr) {
			  if (attr === 'idCardValidDateEnd' || attr === 'validEndDate') {
			    this.cancelText = '长期'
			  } else {
			    this.cancelText = ''
			  }
			
			  if (this.form[attr] && this.form[attr] !== '长期') this.setDateTime = this.form[attr]
			  else this.setDateTime = dateFormat()
			
			  if (this.set !== attr) this.$refs.dateTime.initData()
			
			  this.set = attr
			  this.$refs.dateTime.show()
			},
			update(e) {
			  Object.assign(this.form, e)
			},
			async seucessFn(){
				let flage = false
				let send = {}
				if(this.type == 0){
					if(!this.form.idCard1 || !this.form.idCard1) {
						uni.showToast({
						  title: '请上传全部证件信息',
						  icon: "none",
						  duration: 2000,
						});
						return false
					}
					send.certificationUrl1 = this.form.idCard1
					send.certificationUrl2 = this.form.idCard2
				}
				if(this.type == 1){
					if(!this.form.licence1) {
						uni.showToast({
						  title: '请上传证件信息',
						  icon: "none",
						  duration: 2000,
						});
						return false
					}
					send.certificationUrl1 = this.form.licence1
					send.certificationUrl2 = ''
				}
				if(this.type == 2){
					if(!this.form.certification) {
						uni.showToast({
						  title: '请上传证件信息',
						  icon: "none",
						  duration: 2000,
						});
						return false
					}
					send.certificationUrl1 = this.form.certification
					send.certificationUrl2 = ''
				}
				if(this.type == 3){
					if(!this.form.vlicencePicture) {
						uni.showToast({
						  title: '请上传证件信息',
						  icon: "none",
						  duration: 2000,
						});
						return false
					}
					send.certificationUrl1 = this.form.vlicencePicture
					send.certificationUrl2 = ''
				}
				if(this.type == 4){
					if(!this.form.bizPicture) {
						uni.showToast({
						  title: '请上传证件信息',
						  icon: "none",
						  duration: 2000,
						});
						return false
					}
					send.certificationUrl1 = this.form.bizPicture
					send.certificationUrl2 = ''
				}
				if(this.type == 5){
					if(!this.form.thirdYearNotMajorAccidentCert) {
						uni.showToast({
						  title: '请上传证件信息',
						  icon: "none",
						  duration: 2000,
						});
						return false
					}
					send.certificationUrl1 = this.form.thirdYearNotMajorAccidentCert
					send.certificationUrl2 = ''
				}
				// 提交审核逻辑
				uni.showLoading({
				  title: '加载中',
				  mask: true
				})
				let time = new Date()
				let year = time.getFullYear()
				let month = time.getMonth() + 1
				let date = time.getDate()
				let newTime = `${year}-${month}-${date}`
				let newSend = {
					targetType: 0,
					targetId: this.id,
					certificationType: this.type,
					certificationValue: '',
					certificationData: newTime,
					certificationValidStartDate: newTime,
				}
				send = Object.assign(send,newSend)
				let [err,res] = await submitCertificatesStatus(send)
				uni.hideLoading()
				// 提交成功后返回
				if(res.data){
				  uni.showToast({
				    title: '提交成功',
				    icon: "none",
				    duration: 3000,
				    success() {
				      uni.navigateBack({
				        delta: 1
				      });
				    }
				  });
				}else{
					uni.navigateBack({})
				}
			}
		}
	}
</script>

<style lang="scss" scoped="true">
	.uploaddocuments {
		width: 100vw;
		height: 100vh;
		overflow-y: scroll;
		box-sizing: border-box;
		padding: 30rpx;
		background-color: #F4F4F4FF;
		.uploaddocuments-conten {
			width: 100%;
			padding: 40rpx;
			border-radius: 18rpx;
			background-color: #FFFFFF;
			.inp-conten {
				width: 100%;
				
			}
			.uploaddocuments-titlt {
				width: 100%;
				font-size: 32rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 700;
				color: #000000E6;
				line-height: 32rpx;
				margin-bottom: 46rpx;
			}
			.uploaddocuments-up {
				width: 100%;
				display: flex;
				justify-content: space-between;
				align-items: center;
				flex-wrap: wrap;
				.uploaddocuments-up-list {
					width: 100%;
					box-sizing: border-box;
					
				}
				.uploaddocuments-up-list:nth-child(2n) {
					margin-left: 30rpx;
				}
			}
		}
		.btn {
			width: 100vw;
			box-sizing: border-box;
			padding: 30rpx;
			background-color: #FFFFFF;
			position: fixed;
			left: 0;
			bottom: 0;
			box-shadow: 0 0rpx 10rpx #eaeaea;
			.sucess-btn {
				width: 100%;
				background: linear-gradient(90deg, #E8C9A2 0%, #D9A96A 100%);
				border-radius: 12rpx;
				font-size: 32rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #865825;
				line-height: 32rpx;
				padding: 32rpx 0;
				text-align: center;
			}
		}
	}
</style>